﻿<%@ Page Title="" Language="C#" MasterPageFile="~/SIPO.master" AutoEventWireup="false" CodeFile="recDevolutionlist.aspx.cs" Inherits="SIPO.Web.Lists.Transaction.recDevolutionlist" %>

<asp:Content ID="Content1" ContentPlaceHolderID="body" Runat="Server">
<link type="text/css" rel="stylesheet" href="<%= ResolveUrl("~/styles/ui.jqgrid.css") %>" />
    <script type="text/javascript" src="<%= ResolveUrl("~/scripts/i18n/grid.locale-id.js") %>"></script>
    <script type="text/javascript" src="<%= ResolveUrl("~/scripts/jquery.jqGrid.min.js") %>"></script>
    <script src="<%= ResolveUrl("~/scripts/helper.js") %>" type="text/javascript"></script>

    <div>
        <button id="btnAdd" type="button" onclick="OpenForm('add');">
            Tambah</button>
        <button id="btnEdit" type="button" onclick="OpenForm('edit');">
            Ubah</button>
    </div>
     <br />
    <br />
    <table id="table" class="scroll" cellpadding="0" cellspacing="0">
    </table>
    <div id="pager" class="scroll">
    </div>
    <br />
    <div id="dialog1" title="Default dialog">
    </div>
    <script type="text/javascript">
        $("#btnAdd").button({ icons: { primary: "ui-icon-plus"} });
        $("#btnEdit").button({ icons: { primary: "ui-icon-pencil"} });
    </script>

     <script type="text/javascript">
     var handlerUrl = '<%= ResolveUrl("~/AjaxHandler/Transaction/recDevolutionhandler.ashx") %>';

         $('#table').jqGrid({
             url: handlerUrl + '?method=getdata',
             datatype: 'json',
             colNames: ['id', 'ID Debitur', 'Nama Debitur', 'Nomor Surat Pelimpahan', 'Tgl. Surat Pelimpahan','Tgl. Pelimpahan','Jumlah Pelimpahan','Catatan','Lampiran'],
             colModel: [
           		{ name: 'DevolutionID', key: true, hidden: true, index: 'DevolutionID', width: 5 },
                { name: 'DebtorID', index: 'DebtorID', width: 150, align: 'center', searchoptions: { sopt: ['eq', 'cn']} },
           		{ name: 'DebtorName', index: 'DebtorName', width: 150, align: 'center', searchoptions: { sopt: ['eq', 'cn']} },
                { name: 'LetterNo', index: 'LetterNo', width: 125, align: 'center', searchoptions: { sopt: ['eq', 'cn']} },
                { name: 'LetterDate', index: 'LetterDate', align: 'center', width: 135, formatter: 'date', formatoptions: { newformat: 'd M Y'} },
                { name: 'DevolutionDate', index: 'DevolutionDate', align: 'center', width: 100, formatter: 'date', formatoptions: { newformat: 'd M Y'} },
                { name: 'DevolutionValues', index: 'DevolutionValues', width: 125, formatter: 'currency', searchtype: 'number', formatoptions: { prefix: '', suffix: '', thousandsSeparator: ',' }, align: 'right' },
                { name: 'Note', index: 'Note', width: 150, align: 'left', searchoptions: { sopt: ['eq', 'cn']} },
                { name: 'DevolutionID', index: 'DevolutionID', align: 'center', width: 100, search: false, formatter: returnMyLink },

           	],
             rowNum: 10,
             id: 'DevolutionID',
             multiselect: true,
             rowList: [5, 10, 15, 20],
             pager: '#pager',
             sortname: 'DebtorID',
             sortorder: "asc",
             viewrecords: true,
             jsonReader: { repeatitems: false },
             height: '100%',
             caption: 'Penyerahan Piutang ke DJKN',
             ajaxGridOptions: { cache: false },
             beforeSelectRow: function (rowid, e) {
                 $('#table').jqGrid('resetSelection');
                 return (true);
             },

             ondblClickRow: function () {
                    OpenForm('edit');
                },
                gridComplete: function () {
                    var ids = $('#table').jqGrid('getDataIDs');
                    for (var i = 0; i < ids.length; i++) {
                        var _id = $('#table').jqGrid('getCell', ids[i], 'DevolutionID');
                        
                        idx = "<a href='javascript:void(0);' class='dialog_link ui-corner-all' title='Status Pelimpahan' onclick=\"onclickButtonDemand('" + _id + "');\"><span class='ui-icon ui-icon-newwin'></span>...</a>";

                        $('#table').jqGrid('setRowData', ids[i], { aksi: idx });
                    }
                }
            });

            function returnMyLink(cellValue, options, rowdata) {
                var handleurlDownload = '<%= ResolveUrl("~/ajaxhandler/loaddata/downloadhandler.ashx?method=downloadDevolution") %>';
                //return "<a target='_blank' href='" + handleurlDownload + "&sanction_id=" + cellValue + "' >download</a>";
                return "<a  href='#' onclick='downloadfile(" + cellValue + ")' >download</a>";
            }
            function downloadfile(cellValue) {
                var obj = new Object();
                obj.DevolutionID = encodeURIComponent(cellValue);
                jsonData = JSON.stringify(obj);

                var handlerUrl = '<%= ResolveUrl("~/ajaxhandler/loaddata/downloadhandler.ashx") %>';

                $.ajax({
                    type: 'POST',
                    url: handlerUrl + "?method=checkfiledevolution",
                    contentType: 'application/json',
                    dataType: 'json',
                    data: jsonData,
                    success: function (data) {
                        if (data.IsSuccess) {
                            var win = window.open(handlerUrl + '?method=downloaddevolution&DevolutionID=' + cellValue, '_blank');
                            win.focus();
                        }
                        else {
                            alert(data.Message);
                        }
                    },
                    error: function (response) {
                        alert('Error saat download file!');
                    }
                });

            }

         $("#table").jqGrid('navGrid', '#pager', { edit: false, add: false, del: false, searchtext: 'Search', refreshtext: 'Refresh' },
        {}, {}, {}, { closeOnEscape: true, multipleSearch: true, closeAfterSearch: true, showQuery: true });

         function OpenForm(ctl) {
             var formUrl = '<%= ResolveUrl("~/forms/Transaction/recDevolutionform.aspx") %>';

             var grid = $('#table');
             var selIds = grid.jqGrid('getGridParam', 'selarrrow'),
                selText = [];
             $.each(selIds, function () {
                 selText.push(grid.jqGrid('getCell', this, 'DevolutionID'));
             });
             if (ctl == 'edit') {
                 if (selIds.length == 0)
                     alert('Pilih data yang mau di edit!');
                 else {
                     if (selIds.length > 1) {
                         alert('Pilih satu record untuk ubah data!');
                     }
                     else if (selIds.length == 1) {
                         $('#dialog1').load(formUrl + '?DevolutionID=' + selText).dialog({ autoOpen: false, modal: true, width: 450, title: 'Ubah Pelimpahan Piutang' });
                         $("#dialog1").dialog('open');
                     }
                 }
             } else if (ctl == 'add') {
                $('#dialog1').load(formUrl).dialog({ autoOpen: false, modal: true, width: 400, title: 'Tambah Pelimpahan Piutang' });
                $("#dialog1").dialog('open');
            }
        }
     </script>
</asp:Content>

